package Servlet.Admin;

import Entity.MyTableData;
import Service.Admin.AccountService;
import Service.Admin.GradingManagementService;
import Service.Admin.Imp.AccountServiceImp;
import Service.Admin.Imp.GradingManagementServiceImp;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/reviewerAssign", "/replyReviewerAssign", "/reviewerAllQuery", "/gradingAdd", "/getTeacherByRole"})
public class GradingManagementServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/reviewerAssign".equals(url)) {
            reviewerAssign(request, response);
        } else if ("/replyReviewerAssign".equals(url)) {
            replyReviewerAssign(request, response);
        } else if ("/reviewerAllQuery".equals(url)) {
            reviewerAllQuery(request, response);
        } else if ("/gradingAdd".equals(url)) {
            gradingAdd(request, response);
        } else if ("/getTeacherByRole".equals(url)) {
            getTeacherByRole(request, response);
        }
    }

    private void getTeacherByRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GradingManagementService gradingManagementService = new GradingManagementServiceImp();
        String roleName = request.getParameter("roleName");
        MyTableData myTableData = gradingManagementService.getTeacherByRole(roleName);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 通过工号分配评阅教师
     */
    private void reviewerAssign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GradingManagementService gradingManagementService = new GradingManagementServiceImp();
        String tno = request.getParameter("tno");
        Integer res = gradingManagementService.reviewerAssign(tno);
        PrintWriter out = response.getWriter();
        out.print(res);
        out.close();
    }

    /**
     * 通过工号分配答辩组教师
     */
    private void replyReviewerAssign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GradingManagementService gradingManagementService = new GradingManagementServiceImp();
        String tno = request.getParameter("tno");
        Integer res = gradingManagementService.replyReviewerAssign(tno);
        PrintWriter out = response.getWriter();
        out.print(res);
        out.close();
    }

    /**
     * 通过工号学号新增评阅教师评分表师生对信息
     */
    private void gradingAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GradingManagementService gradingManagementService = new GradingManagementServiceImp();
        String tno = request.getParameter("tno");
        String sno = request.getParameter("sno");
        Integer res = gradingManagementService.gradingAdd(tno, sno);
        PrintWriter out = response.getWriter();
        out.print(res);
        out.close();
    }


    /**
     * 通过工号查询教师角色
     */
    private void reviewerAllQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GradingManagementService gradingManagementService = new GradingManagementServiceImp();
        int type = Integer.parseInt(request.getParameter("type")); // 1为评阅教师  2 为答辩组教师
        MyTableData myTableData = gradingManagementService.reviewerAllQuery(type);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
